package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.mapper.RoomMapper;
import com.zzyl.service.BedService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.BedQueryVo;
import com.zzyl.vo.BedVo;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class BedServiceImpl implements BedService {

    private final BedMapper bedMapper;
    private final RoomMapper roomMapper;

    public BedServiceImpl(BedMapper bedMapper, RoomMapper roomMapper) {
        this.bedMapper = bedMapper;
        this.roomMapper = roomMapper;
    }


    // 新增床位
    @Override
    public int createBed(BedDto bedDto) {
        // 1.校验参数
        // 前端传入来的dto不会为空
        String bedNumber = bedDto.getBedNumber();
        Integer sort = bedDto.getSort();
        // 前端自动传过来的(roomId->code 1->101 2->102 3->103 ... 7->107 8->201)
        Long roomId = bedDto.getRoomId();
        // 床位编号，房间id，排序 三个数据不能为空
        if (ObjectUtil.isEmpty(bedNumber) || ObjectUtil.isEmpty(roomId) || ObjectUtil.isEmpty(sort)) {
            throw new BaseException(BasicEnum.PARAM_IS_NULL);
        }

        // 2.业务处理
        // 【判断1】 判断房号roomId是否存在(房间号需要在room表中查找到)
        if (ObjectUtil.isEmpty(roomMapper.selectById(roomId))) {
            throw new BaseException(BasicEnum.ROOM_CODE_NO_EXIST);
        }
        // 【判断2】 判断当前房间中该床位号是否已存在
        Bed bed = bedMapper.selectByBedNumber(bedNumber);
        if (ObjectUtil.isNotEmpty(bed)) {
            throw new BaseException(BasicEnum.BED_NUMBER_EXIST);
        }
        // 【判断3】 判断该房间是否已达到最大容纳床位数(判断房间的类型是单人还是双人)
        // 先获取到当前房间的类型(sort->typeName 1->特护房 2->普双 3->普单 4->豪双 5->豪单 6->双套 7->单套)
        Integer roomType = roomMapper.selectById(roomId).getSort();
        // 获取当前房间已添加的床位数
        List<Bed> bedList = bedMapper.selectByRoomId(roomId);
        if (roomType == 1 || roomType == 2 || roomType == 4 || roomType == 6) {
            // 双人房间
            if (bedList.size() >= 2) {
                throw new BaseException(BasicEnum.ROOM_IS_FULL);
            }
        } else {
            // 单人房
            if (!bedList.isEmpty()) {
                throw new BaseException(BasicEnum.ROOM_IS_FULL);
            }
        }

        // 3.封装数据
        // 对象拷贝
        Bed newBed = BeanUtil.toBean(bedDto, Bed.class);
        // TODO 公共字段填充
        newBed.setCreator("admin");
        newBed.setCreateBy(1L);
        newBed.setCreateTime(LocalDateTime.now());
        newBed.setUpdateTime(LocalDateTime.now());
        newBed.setBedStatus(0);

        return bedMapper.insertBed(newBed);
    }

    // 删除床位
    @Override
    public int deleteBed(Long id) {
        // 若该床位下已绑定老人 (状态为0为未入住，可以删除)
        // 根据床位id，查询床位信息
        Bed bed = bedMapper.selectById(id);
        if (ObjectUtil.isEmpty(bed)) {
            throw new BaseException(BasicEnum.PARAM_IS_NULL);
        }
        if (bed.getBedStatus() != 0) {
            throw new BaseException(BasicEnum.BED_IS_BIND);
        } else {
            return bedMapper.deleteById(id);
        }
    }

    // 更新床位
    @Override
    public int updateBed(BedDto dto) {
        // 前端值能修改两个数据：床位编号，排序(这两个数据不能为空)
        String bedNumber = dto.getBedNumber();
        Integer sort = dto.getSort();
        if (ObjectUtil.isEmpty(bedNumber) || Objects.isNull(sort)) {
            throw new BaseException(BasicEnum.PARAM_IS_NULL);
        }

        // 补全数据
        Bed bed = new Bed();
        BeanUtil.copyProperties(dto, bed);
        bed.setUpdateBy(1L);
        bed.setUpdateTime(LocalDateTime.now());

        try {
            return bedMapper.updateBed(bed);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_NUMBER_EXIST);
        }

    }

    // 根据床位id查询床位信息
    @Override
    public BedQueryVo selectById(Long id) {
        Bed bed = bedMapper.selectById(id);
        // 数据库中找到的是Bed对象(实体) -> 转换为 -> BedQueryVo对象
        return BeanUtil.toBean(bed, BedQueryVo.class);
    }

    // 根据房间id查询床位信息
    @Override
    public List<BedVo> getBedsByRoomId(Long id) {
        List<BedVo> beds = bedMapper.getBedsByRoomId(id);
        return beds;
    }


}

